define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Agent, $window, $ionicSlideBoxDelegate, $ionicSideMenuDelegate, GoPage, $ionicPopup, GoDialog, $timeout, GoAddress, Auth,GoCatTypeAuth) {
		$scope.isHide = true; //默认隐藏
		$scope.isLoad = false; //可以加载
		$scope.scope = new Object();//数据模型
		$scope.input = new Object();
		var brand = new Array();
		$scope.info_id = '';
		$scope.scope.annual_sales = '';
		$scope.scope.stimated_sales = '';
		$scope.scope.dealer_num = '';
		$scope.scope.brand = '';
		$scope.scope.brand_select = {id:0,name:"请选择品牌"};
		$scope.scope.cat_type_id = "";//品类
		$scope.scope.apply_desc = '';
		$scope.region = '';
		$scope.scope.region1 = '';
		$scope.scope.region2 = '';
		$scope.scope.region3 = '';
		$scope.scope.key = '';
		$scope.scope.region = '';
		$scope.input.push_agent_nPage = 1;
		$scope.input.push_agenter_nPage = 1;
		$scope.input.agent_nPage = 1;
		$scope.input.agenter_nPage = 1;
		$scope.brand_list = new Array();
		/***省市区三级联动***/
		$scope.input.province = {id:0,name:"请选择省份"}; //省市区三级联动
		$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
		$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		$scope.count_f = null;
		$scope.count_s = null;
		$scope.count_t = null;
		$scope.count_fo = null;
		//下拉侦听，目前只能省市区三级联动
		$scope.listen_province = function(data, obj) {
			$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
			$scope.input.district_items = null;
		};
		$scope.listen_city = function(data, obj) {
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		};
		//定义绑定
		var items = new Array();
		var obj1 = { name: 'input.province' };
		var obj2 = { name: 'input.city' };
		var obj3 = { name: 'input.district' };
		items.push(obj1);
		items.push(obj2);
		items.push(obj3);
		GoAddress.init_load($scope, items);
		var myPopup_detail;
		$scope.input.keywords = "";
		//滑动时候触发
		$scope.selectIndex = 2;//由于把前面2个tab页面注释了，所以这里从2开始
		$scope.slideIndex = 2;//定位到第3个tab
//		$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide($scope.selectIndex);
		//计算滚动条高度
		$scope.$on('$ionicView.afterEnter', function(){
				var lan = 0; //这是状态栏，只有ios的APP才有
		var tab = 0;
		var bars = document.getElementById("nav-bar-title");
		var bar = ((bars != undefined) ? 44 : 0);
		if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) { lan = 20; }
		//整体高度
		var all = $window.innerHeight;
		var agent_top1 = document.getElementById("agent-top1").clientHeight;
		$scope.agent_myObj = { //滚动条可视范围
			"height": all - lan - bar - tab - agent_top1 + "px"
		};
		$scope.init_data();
			},false)
		$scope.isLoad = false;
		//地图初始化
		$scope.init_BMap = function() {
			//			var map = new BMap.Map("container"); // 创建地图实例  
			var point = new BMap.Point(116.404, 39.915); // 创建点坐标  
			map.centerAndZoom(point, 15); // 初始化地图，设置中心点坐标和地图级别  
			map.addControl(new BMap.NavigationControl());
			map.addControl(new BMap.ScaleControl());
			//手动定位
			$scope.doLocation();
		};
		//手动定位
		$scope.doLocation = function() {
			var geolocation = new BMap.Geolocation();
			geolocation.getCurrentPosition(function(r) {
				if(this.getStatus() == BMAP_STATUS_SUCCESS) {
					var geoc = new BMap.Geocoder();
					geoc.getLocation(r.point, function(rs) {
						$scope.localaddress = rs.address;
					});
				} else {
					GoDialog.alert('提示','failed' + this.getStatus());
				}
			}, {
				enableHighAccuracy: true
			})
		};
		//初始加载
		//		$scope.init_BMap();

		//加载数据
		$scope.init_data = function() {
//			Agent.get_agent(function(data) {
//				$scope.agent = data;
//			})
			//获取找代理商列表数据
			Agent.agentdemand_list(1, "", "", "", 1, function(data) {
				if(data != null)
					$scope.listData_agent = data;
				else
					$scope.listData_agent = new Object();
				$scope.input.agent_nPage = 1;
			});
			//获取找经销商列表数据
			Agent.agentdemand_list(1, "", "", "", 2, function(data) {
				if(data != null)
					$scope.listData_agenter = data;
				else
					$scope.listData_agenter = new Object();
				$scope.input.agenter_nPage = 1;
			});
			
			//推送信息一览
			Agent.agentpush_list(1, "", "", "", 1, function(data) {
				console.log(data)
				if(data != null)
					$scope.push_agent = data;
				else
					$scope.push_agent = new Object();
				$scope.input.push_agent_nPage = 1;
			});
			Agent.agentpush_list(1, "", "", "", 2, function(data) {
				if(data != null)
					$scope.push_agenter = data;
				else
					$scope.push_agenter = new Object();
				$scope.input.push_agenter_nPage = 1;
			});
			//推送的详细信息
			Agent.agentpush_detail('', function(data) {
				$scope.pushDataDetail = data;

			});
			//品牌
			Agent.brand_list(function(data) {
				if(data != null)
					$scope.brand_list = data.brand;
			})
		};
		
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
			//判断关闭
			if($ionicSideMenuDelegate.isOpenRight()) {
				$ionicSideMenuDelegate.toggleRight();
			}
		};
		$scope.slideChanged = function(index) {//手指滑动
			$scope.selectIndex = index;
		};
		//侧边菜单
		$scope.rightSideMenu = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
		
		//获取品类id
		$scope.getCatId = function(){
			
			if($scope.input.cat3&&$scope.input.cat3.id!=0) {
				$scope.scope.cat_type_id = $scope.input.cat1.id + "," +$scope.input.cat2.id + "," + $scope.input.cat3.id;
			} else if($scope.input.cat2&&$scope.input.cat2.id!=0) {
				$scope.scope.cat_type_id = $scope.input.cat1.id + "," +$scope.input.cat2.id;
			} else if($scope.input.cat1&&$scope.input.cat1.id!=0) {
				$scope.scope.cat_type_id = $scope.input.cat1.id;
			}else{
				$scope.scope.cat_type_id = "";
			}
		}
		
		//查询
		$scope.doSearch = function() {
			$scope.get_region();
			if($scope.selectIndex == 0 || $scope.selectIndex == 1){
				Agent.agentdemand_list(1, $scope.scope.key, $scope.scope.brand, $scope.scope.region, $scope.selectIndex + 1, function(data) {
					if(data != null) {
						if($scope.selectIndex == 0) {
							$scope.listData_agent = data;
						} else {
							$scope.listData_agenter = data;
						}
					} else {
						if($scope.selectIndex == 0) {
							$scope.listData_agent = new Object();
						} else {
							$scope.listData_agenter = new Object();
						}
					}
					if($scope.selectIndex == 0) {
						$scope.input.agent_nPage = 1;
					} else {
						$scope.input.agenter_nPage = 1;
					}
				});
			}else{		//function(nPage,key,cat_type_id,region_id,push_kind,callback,calldone)
				$scope.getCatId();//获取品类id
				Agent.agentpush_list(1, $scope.scope.key, $scope.scope.cat_type_id, $scope.scope.region, $scope.selectIndex - 1, function(data) {
					$scope.pushDataList = data;
					console.log(data)
					if(data != null) {
						if($scope.selectIndex == 2) {
							$scope.push_agent = data;
						} else {
							$scope.push_agenter = data;
						}
					} else {
						if($scope.selectIndex == 2) {
							$scope.push_agent = new Object();
						} else {
							$scope.push_agenter = new Object();
						}
					}
					if($scope.selectIndex == 2) {
						$scope.input.push_agent_nPage = 1;
					} else {
						$scope.input.push_agenter_nPage = 1;
					}
				});
			}	
			$ionicSideMenuDelegate.toggleRight();
		};
		
		//取消
		$scope.doCancel = function() {
			$ionicSideMenuDelegate.toggleRight();
		};

		$scope.onshow_detail = function(value, region, store_name, store_id) {
			GoPage.go_states("myproductdetail", {
				'detail_id': value,
				'region': region,
				'store_name': store_name,
				'store_id': store_id
			});
		};

		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			if($scope.selectIndex == 0 || $scope.selectIndex == 1)
				Agent.agentdemand_list(1, $scope.scope.key, $scope.scope.brand, $scope.scope.region, $scope.selectIndex + 1, function(data) {
					if(data != null) {
						$scope.listData = data;
						if($scope.selectIndex == 0) {
							$scope.listData_agent = data;
						} else {
							$scope.listData_agenter = data;
						}
					} else {
						if($scope.selectIndex == 0) {
							$scope.listData_agent = new Object();
						} else {
							$scope.listData_agenter = new Object();
						}
					}
					if($scope.selectIndex == 0) {
						$scope.input.agent_nPage = 1;
					} else {
						$scope.input.agenter_nPage = 1;
					}

				}, function() {
					$timeout(function() {
						$scope.isLoad = false;
						$scope.$broadcast('scroll.refreshComplete');
					}, 1500);
				});
			else
				Agent.agentpush_list(1, $scope.scope.key, $scope.scope.cat_type_id, $scope.scope.region, $scope.selectIndex - 1, function(data) {
					$scope.pushDataList = data;
					if(data != null) {
						if($scope.selectIndex == 2) {
							$scope.push_agent = data;
						} else {
							$scope.push_agenter = data;
						}
					} else {
						if($scope.selectIndex == 2) {
							$scope.push_agent = new Object();
						} else {
							$scope.push_agenter = new Object();
						}
					}
					if($scope.selectIndex == 2) {
						$scope.input.push_agent_nPage = 1;
					} else {
						$scope.input.push_agenter_nPage = 1;
					}
				}, function() {
					$timeout(function() {
						$scope.isLoad = false;
						$scope.$broadcast('scroll.refreshComplete');
					}, 1500);
				});
		};
		//上拉加载更多必须定义

		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			if($scope.selectIndex == 0) {
				if($scope.listData_agent.agenterdemand == undefined)
					$scope.input.agent_nPage = 0;
				else
				if($scope.listData_agent.page.count <= $scope.listData_agent.agenterdemand.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.listData_agent.page.count<$scope.input.agent_nPage*5){
					$scope.input.agent_nPage -=1;
					$scope.count_f = $scope.input.agent_nPage *5;
						}else{
						$scope.count_f = null;
					}
				}
				Agent.agentdemand_list($scope.input.agent_nPage + 1, $scope.scope.key, $scope.scope.brand, $scope.scope.region, 1, function(data) {
					if(data.agenterdemand.length > 0) {
						if($scope.listData_agent.agenterdemand != undefined) {
							if($scope.count_f!=null)
							$scope.listData_agent.agenterdemand.splice($scope.count_f,$scope.listData_agent.page.count-$scope.count_f);
							$scope.listData_agent.agenterdemand = $scope.listData_agent.agenterdemand.concat(data.agenterdemand);
							$scope.input.agent_nPage += 1;
						} else {
							$scope.listData_agent = data;
							$scope.input.agent_nPage = 1;
						}
					}
				}, function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			} else if($scope.selectIndex == 1) {
				if($scope.listData_agenter.agenterdemand == undefined)
					$scope.input.agenter_nPage = 0;
				else
				if($scope.listData_agenter.page.count <= $scope.listData_agenter.agenterdemand.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.listData_agenter.page.count<$scope.input.agenter_nPage*5){
					$scope.input.agenter_nPage -=1;
					$scope.count_s = $scope.input.agenter_nPage *5;
						}else{
						$scope.count_s = null;
					}
				}
				Agent.agentdemand_list($scope.input.agenter_nPage + 1, $scope.scope.key, $scope.scope.brand, $scope.scope.region, 2, function(data) {
					if(data.agenterdemand.length > 0) {
						if($scope.listData_agenter.agenterdemand != undefined) {
							if($scope.count_s!=null)
							$scope.listData_agenter.agenterdemand.splice($scope.count_s,$scope.listData_agenter.page.count-$scope.count_s);
							$scope.listData_agenter.agenterdemand = $scope.listData_agenter.agenterdemand.concat(data.agenterdemand);
							$scope.input.agenter_nPage += 1;
						} else {
							$scope.listData_agenter = data;
							$scope.input.agenter_nPage = 1;
						}
					}
				}, function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			} else if($scope.selectIndex == 2) {
				if($scope.push_agent.agentpush == undefined){
					$scope.push_agent_nPage = 0;
				}else if($scope.push_agent.page.count <= $scope.push_agent.agentpush.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.push_agent.page.count<$scope.input.push_agent_nPage*5){
					//$scope.input.push_agent_nPage -=1;
					$scope.count_t = $scope.input.push_agent_nPage *5;
						}else{
						$scope.count_t = null;
					}
				}
				Agent.agentpush_list($scope.input.push_agent_nPage + 1, $scope.scope.key, $scope.scope.cat_type_id, $scope.scope.region, 1, function(data) {
					if(data.agentpush.length > 0) {
						if($scope.push_agent.agentpush != undefined) {
							if($scope.count_t!=null)
							$scope.push_agent.agentpush.splice($scope.count_t,$scope.push_agent.page.count-$scope.count_t);
							$scope.push_agent.agentpush = $scope.push_agent.agentpush.concat(data.agentpush);
							$scope.input.push_agent_nPage += 1;
						} else {
							$scope.push_agent = data;
							$scope.push_agent_nPage = 1;
						}
					}
				}, function() {
					$timeout(function() {
						$scope.isLoad = false;
						$scope.$broadcast('scroll.infiniteScrollComplete');
					}, 1500);
				});
			} else {
				if($scope.push_agenter.agentpush == undefined)
					$scope.input.push_agenter_nPage = 0;
				else
				if($scope.push_agenter.page.count <= $scope.push_agenter.agentpush.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.push_agenter.page.count<$scope.input.push_agenter_nPage*5){
					$scope.input.push_agenter_nPage -=1;
					$scope.count_fo = $scope.input.push_agenter_nPage *5;
						}else{
						$scope.count_fo = null;
					}
				}
				Agent.agentpush_list($scope.input.push_agenter_nPage + 1, $scope.scope.key, $scope.scope.cat_type_id, $scope.scope.region, 2, function(data) {
					if(data.agentpush.length > 0) {
						if($scope.push_agenter.agentpush != undefined) {
							if($scope.count_fo!=null)
							$scope.push_agenter.agentpush.splice($scope.count_fo,$scope.push_agenter.page.count-$scope.count_fo);
							$scope.push_agenter.agentpush = $scope.push_agenter.agentpush.concat(data.agentpush);
							$scope.input.push_agenter_nPage += 1;
						} else {
							$scope.push_agenter = data;
							$scope.input.push_agenter_nPage = 1;
						}
					}
				}, function() {
					$timeout(function() {
						$scope.isLoad = false;
						$scope.$broadcast('scroll.infiniteScrollComplete');
					}, 1500);
				});
			}

		};

		//隐藏弹窗
		$scope.hide_apply = function(id) {
			$scope.is_show = true;
			if(id == 1)

				if($scope.scope.annual_sales > 0 && $scope.scope.stimated_sales > 0 && $scope.scope.apply_desc)

					Agent.agentdemand_apply($scope.info_id, $scope.scope.annual_sales, $scope.scope.stimated_sales, $scope.scope.dealer_num, $scope.input.brand, $scope.scope.apply_desc, function(data) {
						if(data.success == true) {
							brand = new Array();
							GoDialog.toast("申请成功");
							$scope.isHide = true;

						} else {
							GoDialog.toast(data.msg);
							$scope.isHide = false;
						}
						$scope.scope.annual_sales = '';
						$scope.scope.stimated_sales = '';
						$scope.scope.dealer_num = '';
						$scope.input.brand = '';
						$scope.scope.apply_desc = '';
					})
			else
				GoDialog.toast("销售额、预计销售额以及申请描述不能为空!");
			else
				$scope.isHide = true;
		};
		//显示弹窗
		$scope.show_apply = function(info_id, store_name, info) {
			$scope.is_show = false;
//			console.log(info);
//			console.log(Auth.check_user_type());
			if(info == 3) {

				if(Auth.check_user_type() != 4) {
					GoDialog.toast("您不是经销商，无权进行申请！");
					$timeout(function() {
						$scope.is_show = true;
					}, 500);

					return;
				}
			} else {
				if(Auth.check_user_type() != 3) {
					GoDialog.toast("您不是代理商，无权进行申请！");
					$timeout(function() {
						$scope.is_show = true;
					}, 500);

					return;
				}
			}
			$scope.info_id = info_id;
			$scope.isHide = false;

		};

		$scope.get_region = function() {
			$scope.scope.region = '';
			if($scope.input.province&$scope.input.province.id!=0) {
				$scope.scope.region = $scope.input.province.id;
			}

			if($scope.input.city&$scope.input.city.id!=0) {
				$scope.scope.region += ',' + $scope.input.city.id;
			}
			if($scope.input.district&$scope.input.district.id!=0) {
				$scope.scope.region += ',' + $scope.input.district.id;
			}
		}

		$scope.showPopup_detail = function(val, index) { //绑定数据时要注意，如果是数组，则通过$index传入下标；如果是json，则传入key
			if($scope.is_show != false) {
				$scope.val = val;
				$scope.detail = val == 0 ? $scope.listData_agent.agenterdemand[index] : $scope.listData_agenter.agenterdemand[index];
				$scope.title = val == 0 ? "制造商详情" : "代理商详情"
				myPopup_detail = $ionicPopup.show({ //show()
					templateUrl: 'templates/modal/agent_detail.html',
					title: $scope.title,
					scope: $scope, //绑定数据，必须写这行
					buttons: [{
						text: "确定",
						type: "button-assertive",
						onTap: function(e) { // 当点击保存按钮时，执行该函数

						}
					}]
				});
			}

		};
		$scope.do_order_tel = function(index, tel) {
			if(index == 1) {
				if(Auth.check_user_type() != 2 && Auth.check_user_type() != 1) {
					GoDialog.toast("您不是制造商或者品牌运营商，无权联系！")
					return;
				}
			} else {
				if(Auth.check_user_type() != 3) {
					GoDialog.toast("您不是代理商无权联系！")
					return;
				}
			}
			$window.location.href = "tel:" + tel;
		}
		$scope.get_brand = function() {
			if($scope.selectIndex == 0) {
				if($scope.listData_agent.brand != undefined) {
					$scope.brand_list = $scope.listData_agent.brand;
				} else {
					$scope.brand_list = null;
				}
			} else if($scope.selectIndex == 1) {
				if($scope.listData_agenter.brand != undefined) {
					$scope.brand_list = $scope.listData_agenter.brand;
				} else {
					$scope.brand_list = null;
				}
			}
		}
		//点击其他区域关闭弹出窗       
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function(event) {　　
			if(event.target.nodeName === 'HTML') {　　　　
				if(myPopup_detail) {　　　　　　
					myPopup_detail.close();　　　　
				}　　　　　
			}
		});
		//		$scope.init_data();
		
		
		/*************新增**************/
		/***分类三级联动***/
		$scope.input.cat1 = { id: 0, name: "请选择大分类" };
		$scope.input.cat2 = { id: 0, name: "请选择中分类" };
		$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		//下拉侦听
		$scope.listen_cat1 = function(data, obj) {
			$scope.input.cat2 = { id: 0, name: "请选择中分类" };
			$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		};
		$scope.listen_cat2 = function(data, obj) {
			$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		};
		//定义绑定
		var items_cat = new Array();
		var obj1_cat = {
			name: 'input.cat1'
		};
		var obj2_cat = {
			name: 'input.cat2'
		};
		var obj3_cat = {
			name: 'input.cat3'
		};
		items_cat.push(obj1_cat);
		items_cat.push(obj2_cat);
		items_cat.push(obj3_cat);
		GoCatTypeAuth.init_load_only($scope, items_cat);
		$scope.doSelectBrand = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择品牌",$scope.brand_list,$scope.scope.brand_select,function(data){
				$scope.scope.brand = data.id;
				$scope.scope.brand_select = data;
			});
		}
		$scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
		}
		$scope.doSelectProvince = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,$scope.input.province,function(data){
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,$scope.input.city,function(data){
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,$scope.input.district,function(data){
				$scope.input.district = data;
			});
		}
		//禁止左右滑动
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
		
		
		
	}

	controller.$inject = ['$scope', 'Agent', '$window', '$ionicSlideBoxDelegate', '$ionicSideMenuDelegate', 'GoPage', '$ionicPopup', 'GoDialog', '$timeout', 'GoAddress', 'Auth',"GoCatTypeAuth"];

	return app.registerController("agentctrl",controller);
});